package com.py.credit.service.impl;

import com.py.credit.constant.LhjcConstant;
import com.py.credit.service.LhjcHongHeimdService;
import com.py.credit.service.RemoteBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class LhjcUploadAjbhShedule {

    @Autowired
    private LhjcHongHeimdService lhjcHongHeimdService;

    @Autowired
    private RemoteBaseService remoteBaseService;

    public synchronized void run() {
        //先查询出所有的部门管理员
        List<LinkedHashMap> managerDeptList = remoteBaseService.getManagerDeptList();

        String[] tables = LhjcConstant.HONG_HEI_MD_TABLE;
        for (LinkedHashMap user : managerDeptList) {
            for (int i = 0; i < tables.length; i++) {
            	
            	String jclb = LhjcConstant.getJclb(tables[i]);    //奖惩类别
            	String ztLb = LhjcConstant.getType(tables[i]);    //主体类别
            	
                boolean flag = true;
                String gzName = lhjcHongHeimdService.getGzNames(user.get("officeId") + "", jclb, "");

                while (flag) {
                    flag = this.uploadHongHeimdList(tables[i], gzName, jclb, ztLb, user.get("USERNAME") + "");
                }
            }
        }
    }

    private boolean uploadHongHeimdList(String tableName, String gzName, String jclb, String type, String username) {
        List<LinkedHashMap> list = new ArrayList<LinkedHashMap>();

        try {
        	List<String> gzNames = Arrays.asList(gzName);
        	
            list = lhjcHongHeimdService.selectUploadHongHeimdList(tableName, gzNames);

            for (LinkedHashMap hongHeimd : list) {
                lhjcHongHeimdService.uploadAdd(tableName, jclb, type, hongHeimd.get("record_id") + "", username);
            }
        } catch (Exception e) {
            return false;
        }

        return list.size() > 0 ? true : false;
    }
}
